import re
import requests
import redis
import json

if __name__=="__main__":
    from spider import Spider
else:
    from .spider import Spider
class Subo_film():
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    r = redis.Redis(host='39.96.170.166', port=6379, password='1', db='1')

    def search_info(self,keyword):   #搜索电影名  返回一个电影详情链接

        url='https://www.subo8988.com/index.php?m=vod-search'
        r=requests.post(url,headers=self.headers,data={'wd':keyword,'submit':'search'})
        r.encoding='utf-8'

        html=r.text
        regex='<li><span class="tt"></span><span class="xing_vb4"><a href="(.*?)" target="_blank">(.*?)</a></span> <span class="xing_vb5">(.*?)</span> <span class="xing_vb6">(.*?)</span></li>'
        info=re.findall(regex,html)
        # print(info)
        search_info=['https://www.subo8988.com'+i[0] for i in info]
        # print(search_info)
        return search_info


    def get_film_info(self,url):   #给一个详情页面的链接  返回相关信息
        if self.r.exists(url):
            print('redis读取')
            return json.loads(self.r.get(url).decode())
        else:

            regex={'img_url':'<img class="lazy" src="(.*?)" alt="(.*?)" />',
                   'film_name':'<h2>(.*?)</h2>',
                   'bie_ming':'<li>别名：<span>(.*?)</span></li>',
                   'dao_yan':'<li>导演：<span>(.*?)</span></li>',
                   'zhu_yan':'<li>主演：<span>(.*?)</span></li>',
                   'lei_xing':'<li>类型：<span>(.*?)</span></li> ',
                   'di_qu':'<li class="sm">地区：<span>(.*?)</span></li>',
                   'yu_yan':'<li class="sm">语言：<span>(.*?)</span></li> ',
                   'shang_ying':'<li class="sm">上映：<span>(.*?)</span></li>',
                   'update':'<li class="sm">更新：<span>(.*?)</span></li> ',
                   'jie_shao':'<div class="vodplayinfo">(.*?)</div>',
                   'bofang_url':'<li><input type="checkbox" name="copy_sel" value="(.*?)" checked="" />(.*?)</li>'
                   }

            info=Spider().get_info(url,**regex)
            show_url=[]
            for i in info['bofang_url']:
                # print(i)
                show_url.append((i[0],str(i[1]).split('$')[0]))
            info['bofang_url']=show_url
            # print(show_url)
            # print(info)
            self.r.set(url, json.dumps(info))   #写入redis
            # print(info)
            return info

    def get_search_film(self,keyword):   #直接给一个电影名然后返回想要的信息
        if self.r.exists(keyword):
            print('redis读取')
            print(json.loads(self.r.get(keyword).decode()))
            return json.loads(self.r.get(keyword).decode())
        else:

            show_url=self.search_info(keyword)
            info_list=[]
            for i in show_url:
                info=self.get_film_info(i)
                info['film_url']=i.replace('https://www.subo8988.com','video/play')
                info_list.append(info)

            # print(info_list)
            self.r.set(keyword, json.dumps(info_list))
            # print(info_list)
            return info_list






if __name__=="__main__":
    # print(Subo_film().search_info('筑梦情缘'))
    # Subo_film().get_film_info('https://www.subo8988.com/?m=vod-detail-id-25099.html')
    Subo_film().get_search_film('绿豆花2019')
